var loading = false;

// 窗口大小改变事件
window.onresize = function() {
	if (loading) {
		_grayWindow(true);
	}
};

// 滚动条事件
window.onscroll = function() {
    if (loading) {
		drawLoadingPicture(getGrayWindow(true));
    }
};

function _grayWindow(flag) { 
	loading = false;
	if(window != top){
		var win = __getFrameWindow();
		if(win != window && win._grayWindow != null) return win._grayWindow(flag);
	}

	var grayLayer = getGrayWindow(flag);
	if (flag) {
		drawLoadingPicture(grayLayer);
	    loading = true;
	} else if(grayLayer != null){
		grayLayer.style.display = "none";
		grayLayer.innerHTML = "";
		loading = false;
	}
};

function drawLoadingPicture(grayLayer) {
	if (!loading) {
		grayLayer.style.display = "";
		grayLayer.innerHTML = "<img id='icon' style='position:absolute; z-index:1003;'>"
			+ "<div id='restore' style='position:absolute;z-index:1003;cursor:hand;color:red;font-size:12pt' onclick='_grayWindow(false);return false;'>关闭</div>"
			+ "<div style='filter:Alpha(Opacity=50); width:100%; height:100%; z-index:1002; background-color:#B0B0B0; cursor:wait'></div>";
	}
	var icon = document.getElementById("icon");
	if (!loading) {
		icon.src = "loading.gif";
	}
	
	var bo = trueBody();
    var dw = bo.clientWidth, dl = bo.scrollLeft, dh = bo.clientHeight, dt = bo.scrollTop;
	var cw = icon.clientWidth, ch = icon.clientHeight;
    icon.style.top = dt + (dh - ch) / 2;
    icon.style.left = dl + (dw - cw) / 2;
	var restore =  document.getElementById("restore");
    restore.style.top = dt + 12;
    restore.style.left = dl + dw - 44;
    bo = grayLayer = icon = restore = null;
};

function getGrayWindow(flag) {
	var div = document.getElementById("grayLayer");
	if(div == null && flag != false){
		div = document.createElement("<div id='grayLayer' style='position:absolute;left:0;top:0;width:" + trueBody().scrollWidth + "px;height:" + trueBody().scrollHeight + "px;z-index:1001;display:none' oncontextmenu='event.cancelBubble=true;return false;'>");
		document.body.appendChild(div);
	}
	return div;
};

function __getFrameWindow() {
	var win = window;
	try{ 
		while (win != top && win.parent.Dialog == null) win = win.parent;
	}catch(E){}
	return win;
};

// 获取真实body
function trueBody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}